//
//  UserWeibosTableViewController.m
//  TLBS-Project
//
//  Created by 123 on 13-12-26.
//  Copyright (c) 2013年 Wei WenRu. All rights reserved.
//

#import "TRWeiboListViewController.h"
#import "WeiboPaser.h"
#import "TRMyRequest.h"
#import "SBJSON.h"
@interface TRWeiboListViewController ()
@property (nonatomic, strong)NSMutableArray *weibos;
@property (nonatomic, strong)NSMutableArray *requests;

@end

@implementation TRWeiboListViewController


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.requests = [NSMutableArray array];
    self.weibos = [NSMutableArray array];
    self.title = self.userInfo.nick;
    
    TRMyRequest *request = [[TRMyRequest alloc]init];
    [self.requests addObject:request];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    [params setObject:@"json" forKey:@"format"];
    [params setObject:@"0" forKey:@"pageflag"];
    [params setObject:@"0" forKey:@"pagetime"];
    [params setObject:@"20" forKey:@"reqnum"];
    [params setObject:@"0" forKey:@"lastid"];
    [params setObject:@"0" forKey:@"type"];
    [params setObject:@"0" forKey:@"contenttype"];
    [params setObject:self.userInfo.openid forKey:@"fopenid"];

    [request requestWithParams:params apiName:@"statuses/user_timeline" httpMethod:@"POST":^(NSString *jsonString) {
        NSDictionary *dic = [[jsonString JSONValue] objectForKey:@"data"];
        NSArray *weibos = [dic objectForKey:@"info"];
        
        for (NSDictionary *weiboDic in weibos) {
           
            Weibo *w = [WeiboPaser paseWeiboByDictionary:weiboDic];
            [self.weibos addObject:w];
        }
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.weibos.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Weibo *w = self.weibos[indexPath.row];
    return [w getHeight] + 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    WeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
     Weibo *weibo = [self.weibos objectAtIndex:indexPath.row];
   cell.weibo = weibo;
    cell.delegate = self;
    
    return cell;
}


@end
